package middleware

import (
	"app/internal/consts"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

// RequestId 在响应中增加请求id
func RequestHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Request.Header.Get(consts.REQUEST_ID)
		if id == "" {
			id = uuid.New().String()
		}
		c.Set(consts.REQUEST_ID, id)
		c.Request.Header.Set(consts.REQUEST_ID, id)
		c.Writer.Header().Set(consts.REQUEST_ID, id)
		c.Next()
	}
}
